<?php
/**
 * Created by PhpStorm.
 * User: River
 * Email: river0428@gmail.com
 * Date: 2018/11/16
 * Time: 上午10:37
 */

namespace Modules\Admin\Main\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Activity;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class ActivityController extends Controller
{
    public function index()
    {
        return view('main::activity', \Request::all());
    }

    public function all()
    {
        $data['items'] = Activity::with('image')
            ->orderBy('end_at', 'desc')->get();
        return success($data);
    }


    public function store(Request $request)
    {
        try {
            $data      = $request->only('image_id', 'title', 'detail', 'end_at', 'created_at');
            $validator = Validator::make($request->all(), [
                'image_id'   => 'required',
                'title'      => 'required',
                'detail'     => 'required',
                'end_at'     => 'required',
                'created_at' => 'required',
            ]);
            if ($validator->fails()) {
                return fail(400, $validator->errors()->first());
            }
            Activity::create([
                'image_id' => $data['image_id'],
                'title'    => $data['title'],
                'detail'   => $data['detail'],
                'end_at'   => Carbon::parse($data['end_at'])->toDateTimeString()
            ]);

            return success();
        } catch (\Exception $e) {
            Log::error($e);
            return fail(400, '添加失败');
        }
    }

    public function update(Request $request, $id)
    {
        try {
            $activity  = Activity::find($id);
            $image_id  = $request->input('image_id');
            $title     = $request->input('title');
            $end_at    = $request->input('end_at');
            $detail    = $request->input('detail');
            $validator = Validator::make($request->all(), [
                'image_id'   => 'required',
                'title'      => 'required',
                'detail'     => 'required',
                'end_at'     => 'required',
                'created_at' => 'required',
            ]);
            if ($validator->fails()) {
                return fail(400, $validator->errors()->first());
            }
            if ($image_id) {
                $activity->image_id = $image_id;
            }
            if ($title) {
                $activity->title = $title;
            }
            if ($end_at) {
                $activity->end_at = Carbon::parse($end_at)->toDateTimeString();
            }
            if ($detail) {
                $activity->detail = $detail;
            }
            if ($request->has('created_at')) {
                $activity->created_at = $request->input('created_at');
            }
            $activity->save();
            return success();
        } catch (\Exception $e) {
            Log::error($e);
            return fail(400, '添加失败');
        }
    }

    public function destroy($id)
    {
        try {
            $activity = Activity::find($id);
            $activity->delete();

            return success();
        } catch (\Exception $e) {
            Log::error($e);
            return fail(400, '删除失败');
        }
    }
}